require_relative './question'
require_relative './answer'

class QuestionsGraph
  def initialize(&block)
    @known_questions = []
    instance_eval(&block)
  end

  def question(id, text, &block)
    @known_questions[id - 1] = Question.new(id, text, &block)
  end

  def routes
    @routes ||= find_routes(@known_questions[0])
  end

  private

  def find_routes(question)
    return question.answers.map{|a| [a]} if question.answers.all?(&:leaf?)
    question.answers.flat_map do |answer|
      next_question = @known_questions[answer.next_question_id - 1]
      sub_routes = find_routes(next_question)
      sub_routes.map{|route| [answer, *route]}
    end
  end
end
